<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="日期" prop="deliveryDate">
        <el-date-picker
          v-model="queryParams.deliveryDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-240px"
        />
      </el-form-item>


      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>

        <el-button
          type="success"
          :loading="exportLoading"
          plain
          @click="handleExport"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-table v-loading="detailLoading" :data="list">
      <el-table-column type="index" label="序号" align="center" width="55" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="partID" align="center" prop="partId" />
      <el-table-column label="订单编号" align="center" prop="orderCode" />
      <el-table-column label="交期" align="center" prop="expiryDateStr" />
      <el-table-column label="产品名称" align="center" prop="name" />
      <el-table-column label="型号" align="center" prop="flex" />
      <el-table-column label="长度" align="center" prop="len" />
      <el-table-column label="JR/INT/SR" align="center" prop="type" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="图档名" align="center" prop="drawing" />
      <el-table-column label="面漆" align="center" prop="topcoat" />
      <el-table-column label="EAN CODE" align="center" prop="eanCode" />
      <el-table-column label="A课已完成数" align="center" prop="deptAFinishCount" />
      <el-table-column label="B课已完成数" align="center" prop="deptBFinishCount" />
      <el-table-column label="C课已完成数" align="center" prop="deptCFinishCount" />
      <el-table-column label="操作" align="center" width="160">
        <template #default="scope">
          <div class="flex items-center justify-center">
            <el-button
              type="primary"
              link
              @click="viewDetail(scope.row.id)"
            >查看
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script lang="ts" setup>
import * as orderApi from '@/api/hcxly/order/Index'
import {listOrderDetail} from "@/api/hcxly/order/Index";
import download from "@/utils/download";

defineOptions({ name: 'productionScheduleReportOrderDetail' })

const dialogVisible = ref(false) // 弹窗的是否展示
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const router = useRouter() // 路由
const { query } = useRoute() // 查询参数
const detailLoading = ref(false) // 表单的加载中
const detailData = ref({} as orderApi.orderVO) // 详情数据
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
let formData = ref({}) // 列表的数据

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const orderId = query.id as unknown as string // 从 URL 传递过来的 id 编号
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderCode: '',
  deliveryDate: []
})

/** 查询列表 */
const getList = async () => {
  detailLoading.value = true
  try {
    queryParams.orderCode = orderId
    const data = await orderApi.listOrderDetail(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    detailLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const { push } = useRouter()
const viewDetail = (id) => {
  router.push({
    name: 'productionScheduleReport3',
    query: {
      id:id
    }
  })
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await orderApi.exportOrderDetail(queryParams)
    download.excel(data, '订单详情信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
